package com.mxl.mvc.cashorder;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.mxl.dao.cashorder.impl.GatewayTransferCardOrderDaoImpl;
import com.mxl.dao.cashorder.model.GatewayTransferCardOrder;

/**
 * 余额卡支付
 * 
 * @author xianglin.mo
 * 
 */
public class GatewayBlaCardPayController extends MultiActionController {

	private GatewayTransferCardOrderDaoImpl gatewayTransferCardOrderDao;

	public ModelAndView index(final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		// 从标准网关页面 获取参数 卡密 卡号 等信息 订单id
		request.setCharacterEncoding("UTF-8");
		return null;
	}

	/**
	 * 查询余额卡信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView query(final HttpServletRequest request,
			final HttpServletResponse response) throws Exception {
		// 从标准网关页面 获取参数 卡密 卡号 等信息 订单id
		request.setCharacterEncoding("UTF-8");
		GatewayTransferCardOrder cardBla = gatewayTransferCardOrderDao
				.queryBla(request.getParameter("cardNo"),
						request.getParameter("cardPwd"));
		StringBuilder str = new StringBuilder();
		if (cardBla == null) {
			str.append("500");
		} else {
			str.append("200").append("@")
					.append(cardBla.getBalance() / 100 + ".00").append("@")
					.append(cardBla.getBossType());
		}
		responseReturnContent(response, str.toString());
		return null;
	}

	/**
	 * 直接返回内容.
	 * 
	 * @param response
	 * @param content
	 */
	private void responseReturnContent(HttpServletResponse response,
			String content) {
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write(content);
			writer.flush();
		} catch (IOException e) {
			logger.error("responseReturnContent error", e);
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public void setGatewayTransferCardOrderDao(
			GatewayTransferCardOrderDaoImpl gatewayTransferCardOrderDao) {
		this.gatewayTransferCardOrderDao = gatewayTransferCardOrderDao;
	}

}
